/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package au.gov.aims.amprototypes;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import org.geotools.data.DataStore;
import org.geotools.data.DataStoreFinder;
import org.geotools.data.FeatureSource;
import org.geotools.data.Query;
import org.geotools.feature.FeatureCollection;
import org.geotools.feature.FeatureIterator;
import org.geotools.referencing.CRS;
import org.opengis.feature.Feature;
import org.opengis.feature.Property;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.feature.simple.SimpleFeatureType;
import org.opengis.referencing.FactoryException;
import org.opengis.referencing.NoSuchAuthorityCodeException;

/**
 *
 * @author Gael Lafond <g.lafond@aims.org.au>
 */
public class LayerFeaturesIndexer {
	private String serverUrl;

	public LayerFeaturesIndexer(String serverUrl) throws IOException, NoSuchAuthorityCodeException, FactoryException {
		this.serverUrl = serverUrl;

		// http://docs.codehaus.org/display/GEOTDOC/WFS+Plugin
		String getCapabilities = this.serverUrl + "?REQUEST=GetCapabilities"; // TODO

		Map<String, String> connectionParameters = new HashMap<String, String>();
		connectionParameters.put("WFSDataStoreFactory:GET_CAPABILITIES_URL", getCapabilities);

		// Step 2 - connection
		DataStore data = DataStoreFinder.getDataStore(connectionParameters);

		// Step 3 - discouvery
		String[] typeNames = data.getTypeNames();
		for (String typeName : typeNames) {
// ignore a few point layers
if (typeName != null && typeName.contains("ea:GBR_AIMS_BRUVS")) continue;

			// Step 4 - target
			FeatureSource<SimpleFeatureType, SimpleFeature> source = data.getFeatureSource(typeName);
			//System.out.println("Metadata Bounds: " + source.getBounds());

			// Step 5 - query
			Query query = new Query();
			query.setCoordinateSystem(CRS.decode("EPSG:4326"));
			FeatureCollection<SimpleFeatureType, SimpleFeature> features = source.getFeatures(query);

			FeatureIterator<SimpleFeature> iterator = features.features();

			System.out.println("########## TYPE [" + typeName + "]");
int found = 0;
			while(iterator.hasNext()){
System.out.println("##### PASS 1");
				try {
					Feature feature = iterator.next();

					System.out.println("##### NOT SCREWED!");
					System.out.println("##### ID: " + feature.getIdentifier().getID());
					System.out.println("##### NAME: " + feature.getName());
					System.out.println("##### TYPE.NAME: " + feature.getType().getName());
					System.out.println("##### VALUES:");
					for (Property property : feature.getValue()) {
						System.out.println("########### " + property.getName() + " [" + property.getType().getName() + "]: " + property.getValue());
					}
					System.out.println("Bounds: " + feature.getBounds());
if (++found >= 5) return; // TEST: Stop after a few
				} catch(Exception e) {
					System.out.println("##### SCREWED! " + e.getMessage());
				}
System.out.println("##### PASS 2");
			}
			iterator.close();
		}
	}

	public void newFeature() {
		// No commit...
		System.out.println("AAA");
	}
}
